#!/usr/bin/env bash

set -e

[ "$DEBUG" == 'true' ] && set -x

DAEMON=sshd

# Copy default config from cache
if [ ! "$(ls -A /etc/ssh)" ]; then
 cp -a /etc/ssh.cache/* /etc/ssh/
fi

# Generate Host keys, if required
if ! ls /etc/ssh/ssh_host_* 1> /dev/null 2>&1; then
  ssh-keygen -A
fi

# Fix permissions, if writable
if [ -w ~/.ssh ]; then
  chown root:root ~/.ssh && chmod 700 ~/.ssh/
fi
if [ -w ~/.ssh/authorized_keys ]; then
  chown root:root ~/.ssh/authorized_keys
  chmod 600 ~/.ssh/authorized_keys
fi

# Warn if no config
if [ ! -e ~/.ssh/authorized_keys ]; then
  echo "WARNING: No SSH authorized_keys found for root"
fi

stop() {
  echo "Received SIGINT or SIGTERM. Shutting down $DAEMON"
  # Get PID
  pid=$(cat /var/run/$DAEMON/$DAEMON.pid)
  # Set TERM
  kill -SIGTERM "${pid}"
  # Wait for exit
  wait "${pid}"
  # Unresigter in Control-M
  /decommission_controlm.sh
  # All done.
  echo "Done."
}

echo "Running $@"
if [ "$(basename $1)" == "$DAEMON" ]; then
  trap stop SIGINT SIGTERM
  $@ &
  pid="$!"
  mkdir -p /var/run/$DAEMON && echo "${pid}" > /var/run/$DAEMON/$DAEMON.pid

  # Register in Control-M
  /run_register_controlm.sh

  wait "${pid}" && exit $?
else
  exec "$@"
fi
